package u3dserver.threads;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import util.LogUtil;

/**
 * 每小时检测一次，看所有在跑的线程有没挂
 * @author zhangqing
 *
 */
public class ThreadManage extends Thread{
	/**在线状态检测*/
	private List<String> list = null;
	private Map<String,Thread> maptThread = null;
	public ThreadManage(){
		list = new ArrayList<>();
		maptThread = new HashMap<>();
		list.add("u3dserver.threads.ThreadOnlineState");
		list.add("u3dserver.threads.ThreadMonsterPatrol");
	}
	
	@Override
	public void run() {
		for(;;){
			for(String classname:list){
				Thread thread = maptThread.get(classname);
				if(thread==null||(!thread.isAlive())){
					try {
						thread = (Thread)Class.forName(classname).newInstance();
					} catch (InstantiationException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
					thread.start();
					maptThread.put(classname, thread);
					LogUtil.info("server", this, "线程启动！"+classname);
				}
			}
			
			try {
				sleep(60*60*3000);//一个小时检测一次线程是否挂了
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}
